home *** CD-ROM | disk | FTP | other *** search
- //========================================================================================
- //
- // File: ContainerDDCmd.cpp
- // Release Version: $ 1.0d11 $
- //
- // Author: Mary Boetcher
- //
- // Copyright: © 1993, 1995 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #include "Container.hpp"
-
- #ifndef CONTAINERDDCMD_H
- #include "ContainerDDCmd.h"
- #endif
-
- #ifndef CONTAINERSEL_H
- #include "ContainerSel.h"
- #endif
-
- #ifndef BASESHP_H
- #include "BaseShp.h"
- #endif
-
- #ifndef CONTAINERPART_H
- #include "ContainerPart.h"
- #endif
-
- #ifndef CONTAINERCLIP_H
- #include "ContainerClip.h"
- #endif
-
- #ifndef CONTAINERLINK_H
- #include "ContainerLink.h"
- #endif
-
- // ----- FrameWork Includes -----
-
- #ifndef FWFRAME_H
- #include "FWFrame.h"
- #endif
-
- #ifndef FWPRESEN_H
- #include "FWPresen.h"
- #endif
-
- //========================================================================================
- // RunTime Info
- //========================================================================================
-
- #ifdef FW_BUILD_MAC
- #pragma segment containercommand
- #endif
-
- //========================================================================================
- // class CDragCommand
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // CDragCommand constructor
- //----------------------------------------------------------------------------------------
- CDragCommand::CDragCommand(Environment* ev,
- CContainerPart* part,
- FW_CFrame* frame,
- CContainerSelection* selection,
- FW_Boolean canUndo)
- : FW_CDragCommand(ev, frame, canUndo),
- fContainerPart(part),
- fContainerSelection(selection),
- fClearedShapeList(NULL)
- {
- }
-
- //----------------------------------------------------------------------------------------
- // CDragCommand destructor
- //----------------------------------------------------------------------------------------
- CDragCommand::~CDragCommand()
- {
- delete fClearedShapeList;
- }
-
- //----------------------------------------------------------------------------------------
- // CDragCommand::SaveUndoState
- //----------------------------------------------------------------------------------------
- void CDragCommand::SaveUndoState(Environment* ev) // Override
- {
- fClearedShapeList = new FW_CPrivOrderedCollection;
-
- // Save shapes that will be dragged
- FW_COrderedCollectionIterator ite(fContainerSelection->GetSelectionCollection());
- for (CBaseShape* shape = (CBaseShape*)ite.First(); ite.IsNotComplete(); shape = (CBaseShape*)ite.Next())
- {
- fClearedShapeList->AddLast(shape);
- }
-
- this->SetMenuStrings(ev, "Undo Move", "Redo Move");
- }
-
- //----------------------------------------------------------------------------------------
- // CDragCommand::UndoIt
- //----------------------------------------------------------------------------------------
-
- void CDragCommand::UndoIt(Environment* ev) // Override
- {
- // ----- Add the dragged shapes back into the part
- FW_COrderedCollectionIterator ite(fClearedShapeList);
- for (CBaseShape* shape = (CBaseShape*)ite.First(); ite.IsNotComplete(); shape = (CBaseShape*)ite.Next())
- {
- shape->RestoreShape(ev, fContainerPart);
- }
-
- // ----- Add the saved shapes to the selection
- this->SelectDraggedShapes(ev);
-
- // ----- Invalidate
- fFrame->GetPresentation(ev)->Invalidate(ev, fContainerSelection->GetUpdateShape());
- }
-
- //----------------------------------------------------------------------------------------
- // CDragCommand::RedoIt
- //----------------------------------------------------------------------------------------
-
- void CDragCommand::RedoIt(Environment* ev) // Override
- {
- this->SelectDraggedShapes(ev); // Select saved shapes
-
- fContainerSelection->ClearSelection(ev); // clear them, again
- }
-
- //----------------------------------------------------------------------------------------
- // CDragCommand::FreeUndoState
- //----------------------------------------------------------------------------------------
- void CDragCommand::FreeUndoState(Environment *ev) // Override
- {
- // Delete the dragged shapes - they're gone forever
- CBaseShape* shape;
-
- //-- First check whether the shapes are promised
- FW_COrderedCollectionIterator ite(fClearedShapeList);
- for (shape = (CBaseShape*)ite.First(); ite.IsNotComplete(); shape = (CBaseShape*)ite.Next())
- {
- shape->CheckPromise(ev, fContainerPart);
- }
-
- //-- Now delete them
- while ((shape = (CBaseShape*)fClearedShapeList->First()) != NULL)
- {
- fClearedShapeList->Remove(shape);
- delete shape;
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CDragCommand::SelectDraggedShapes
- //----------------------------------------------------------------------------------------
-
- void CDragCommand::SelectDraggedShapes(Environment* ev)
- {
- // First, empty the selection
- fContainerSelection->CloseSelection(ev);
-
- // Add the shapes in fClearedShapeList to the selection
- FW_COrderedCollectionIterator ite(fClearedShapeList);
- for (CBaseShape* shape = (CBaseShape*)ite.First(); ite.IsNotComplete(); shape = (CBaseShape*)ite.Next())
- {
- fContainerSelection->AddToSelection(ev, shape, FALSE);
- }
- }
-
- //========================================================================================
- // class CDropCommand
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // CDropCommand constructor
- //----------------------------------------------------------------------------------------
- CDropCommand::CDropCommand(Environment* ev,
- CContainerPart* part,
- FW_CFrame* frame,
- ODDragItemIterator* dropInfo,
- ODFacet* facet,
- const FW_CPoint& windowPoint,
- FW_Boolean canUndo)
- : FW_CDropCommand(ev, (FW_CPart*)part, frame, dropInfo, facet, windowPoint, canUndo),
- fContainerPart(part)
- {
- fContainerSelection = (CContainerSelection*)frame->GetPresentation(ev)->GetSelection(ev);
- fDropDelta = FW_kZeroPoint;
- fDroppedShapeList = new FW_CPrivOrderedCollection;
- fSavedLink = NULL;
- }
-
- //----------------------------------------------------------------------------------------
- // CDropCommand destructor
- //----------------------------------------------------------------------------------------
- CDropCommand::~CDropCommand()
- {
- delete fDroppedShapeList;
- }
-
- //----------------------------------------------------------------------------------------
- // CDropCommand::DoDrop
- //----------------------------------------------------------------------------------------
- FW_Boolean CDropCommand::DoDrop(Environment* ev,
- ODStorageUnit* dropSU,
- const FW_CPoint& mouseOffset,
- const FW_CPoint& dropPoint,
- FW_Boolean isDropMove)
- {
- fContainerSelection->CloseSelection(ev);
-
- FW_Boolean result = FW_CDropCommand::DoDrop(ev, dropSU, mouseOffset, dropPoint, isDropMove);
-
- if (result)
- {
- fContainerPart->SetTool(ev, kSelectTool);
-
- FW_CPoint newPosition(dropPoint.x - mouseOffset.x, dropPoint.y - mouseOffset.y);
-
- FW_CRect box;
- fContainerSelection->GetDragRect(box);
-
- fContainerSelection->OffsetSelection(ev, newPosition.x - box.left, newPosition.y - box.top);
-
- ODShape* updateShape = fContainerSelection->GetUpdateShape();
-
- // ----- Calculate clip -----
- CContainerFacetClipper facetClipper(ev, fContainerPart);
- facetClipper.Clip(ev, fContainerSelection->GetPresentation(ev), updateShape);
-
- // ----- Invalidate -----
- fFrame->GetPresentation(ev)->Invalidate(ev, updateShape);
-
- // ----- If I am not the active part I should not have a selection -----
- if (!fContainerPart->IsActive(ev))
- fContainerSelection->CloseSelection(ev);
- }
-
- return result;
- }
-
- //----------------------------------------------------------------------------------------
- // CDropCommand::DoDroppedInSameFrame
- //----------------------------------------------------------------------------------------
- FW_Boolean CDropCommand::DoDroppedInSameFrame(Environment* ev,
- ODStorageUnit* dropSU,
- const FW_CPoint& originPoint,
- const FW_CPoint& dropPoint)
- {
- FW_UNUSED(dropSU);
-
- fDropDelta = dropPoint - originPoint;
- this->OffsetSelection(ev, fDropDelta);
-
- return TRUE;
- }
-
- //----------------------------------------------------------------------------------------
- // CDropCommand::SaveRedoState
- //----------------------------------------------------------------------------------------
- void CDropCommand::SaveRedoState(Environment* ev) // Override
- {
- if (fCommandID != kODCommandPasteAs) // Paste As doesn't need to save shapes
- {
- // Save shapes that were just dropped
- FW_COrderedCollectionIterator ite(fContainerSelection->GetSelectionCollection());
- for (CBaseShape* shape = (CBaseShape*)ite.First(); ite.IsNotComplete(); shape = (CBaseShape*)ite.Next())
- {
- fDroppedShapeList->AddLast(shape);
- }
- }
-
- // Set Undo/Redo menu strings
- if (fCommandID == kODCommandPasteAs)
- this->SetMenuStrings(ev, "Undo Paste As", "Redo Paste As");
- else if (this->IsDragMoveInSameFrame(ev))
- this->SetMenuStrings(ev, "Undo Move", "Redo Move");
- else
- this->SetMenuStrings(ev, "Undo Drop", "Redo Drop");
- }
-
- //----------------------------------------------------------------------------------------
- // CDropCommand::UndoIt
- //----------------------------------------------------------------------------------------
- void CDropCommand::UndoIt(Environment* ev) // Override
- {
- if (fCommandID == kODCommandPasteAs)
- {
- GetContainerLinkManager(ev)->UndoPasteAs(ev, fSavedLink);
- }
- else if (this->IsDragMoveInSameFrame(ev)) // it's a drag-move
- {
- // select dropped shapes and move them back to the original position
- this->SelectDroppedShapes(ev);
- this->OffsetSelection(ev, -fDropDelta);
- fContainerSelection->SelectionChanged(ev); // need more than this for recontainer???
- }
- else
- {
- // select dropped shapes and remove them from the document
- this->SelectDroppedShapes(ev);
- fContainerSelection->ClearSelection(ev);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CDropCommand::RedoIt
- //----------------------------------------------------------------------------------------
- void CDropCommand::RedoIt(Environment* ev) // Override
- {
- if (fCommandID == kODCommandPasteAs)
- {
- GetContainerLinkManager(ev)->RedoPasteAs(ev, fSavedLink);
- this->OffsetSelection(ev, -fDropDelta);
- }
- else if (this->IsDragMoveInSameFrame(ev)) // it's a drag-move
- {
- // select dropped shapes and move them to new position
- this->SelectDroppedShapes(ev);
- this->OffsetSelection(ev, fDropDelta);
- }
- else // dropped shapes are copies
- {
- // add dropped shapes back into document
- this->RestoreDroppedShapes(ev);
- fFrame->GetPresentation(ev)->Invalidate(ev, fContainerSelection->GetUpdateShape());
- }
- fContainerSelection->SelectionChanged(ev);
- }
-
- //----------------------------------------------------------------------------------------
- // CDropCommand::RestoreDroppedShapes
- //----------------------------------------------------------------------------------------
- void CDropCommand::RestoreDroppedShapes(Environment* ev)
- {
- // Add the dropped shapes back into the part
- FW_COrderedCollectionIterator ite(fDroppedShapeList);
- for (CBaseShape* shape = (CBaseShape*)ite.First(); ite.IsNotComplete(); shape = (CBaseShape*)ite.Next())
- {
- shape->RestoreShape(ev, fContainerPart);
- }
-
- // Add the saved shapes to the selection
- this->SelectDroppedShapes(ev);
- }
-
- //----------------------------------------------------------------------------------------
- // CDropCommand::SelectDroppedShapes
- //----------------------------------------------------------------------------------------
- void CDropCommand::SelectDroppedShapes(Environment* ev)
- {
- // First, empty the selection
- fContainerSelection->CloseSelection(ev);
-
- // Add the shapes in fDroppedShapeList to the selection
- FW_COrderedCollectionIterator ite(fDroppedShapeList);
- for (CBaseShape* shape = (CBaseShape*)ite.First(); ite.IsNotComplete(); shape = (CBaseShape*)ite.Next())
- {
- fContainerSelection->AddToSelection(ev, shape, FALSE);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CDropCommand::OffsetSelection
- //----------------------------------------------------------------------------------------
- void CDropCommand::OffsetSelection(Environment* ev, const FW_CPoint& delta)
- {
- if (delta != FW_kZeroPoint)
- {
- fFrame->GetPresentation(ev)->Invalidate(ev, fContainerSelection->GetUpdateShape());
- fContainerSelection->OffsetSelection(ev, delta.x, delta.y);
- fFrame->GetPresentation(ev)->Invalidate(ev, fContainerSelection->GetUpdateShape());
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CDropCommand::CommitUndone
- //----------------------------------------------------------------------------------------
- void CDropCommand::CommitUndone(Environment* ev) // Override
- {
- if (fCommandID == kODCommandPasteAs)
- {
- GetContainerLinkManager(ev)->CommitUndone(ev, fSavedLink);
- }
- else
- FW_CDropCommand::CommitUndone(ev);
- }
-
- void CDropCommand::FreeRedoState(Environment* ev) // Override
- {
- this->SelectDroppedShapes(ev);
- fContainerSelection->DeleteSelection(ev);
- }
-
- //----------------------------------------------------------------------------------------
- // CDropCommand::GetContainerLinkManager
- //----------------------------------------------------------------------------------------
- CContainerLinkManager* CDropCommand::GetContainerLinkManager(Environment* ev) const
- {
- return (CContainerLinkManager*) fPart->GetLinkManager(ev);
- }
-
- //----------------------------------------------------------------------------------------
- // CDropCommand::DoDroppedPasteAs
- //----------------------------------------------------------------------------------------
- void CDropCommand::DoDroppedPasteAs(Environment* ev,
- const FW_CPoint& originPoint,
- const FW_CPoint& dropPoint)
- {
- fDropDelta = dropPoint - originPoint;
-
- // Save information about newly-created link
- CContainerLinkManager* linkMgr = GetContainerLinkManager(ev);
- fSavedLink = linkMgr->GetLatestSubscriber(ev);
- fSavedLink->SetUpdateOffset(ev, fDropDelta);
-
- if (fDropDelta != FW_kZeroPoint)
- {
- // invalidate and clear the current selection
- fFrame->GetPresentation(ev)->Invalidate(ev, fContainerSelection->GetUpdateShape());
- fContainerSelection->CloseSelection(ev); // empty the selection
-
- // select the dropped shapes
- linkMgr->SelectSubscribedShapes(ev, fSavedLink);
-
- // offset the dropped shapes
- fContainerSelection->OffsetSelection(ev, fDropDelta.x, fDropDelta.y);
- fFrame->GetPresentation(ev)->Invalidate(ev, fContainerSelection->GetUpdateShape());
- }
- }
-